package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.bean.OpenCodeBean;
import m.jclecai.com.bean.klpk.bean.PokerBean;
import m.jclecai.com.contents.LotteryType;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.KlpkUtils;

/**
 * Created by hp on 2017/3/13.
 */

public class LotteryAnnouncementAdapter extends BaseAdapter {

    private Context context;
    private List<OpenCodeBean> bean;

    private LinearLayout.LayoutParams lp;

    public LotteryAnnouncementAdapter(Context context, List<OpenCodeBean> bean){
        this.context = context;
        this.bean = bean;
    }

    @Override
    public int getCount() {
        return bean.size();
    }

    @Override
    public Object getItem(int position) {
        return bean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.lottery_announcement_item,null);
            holder.tv_ball_one = (TextView) convertView.findViewById(R.id.tv_ball_one);
            holder.tv_ball_two = (TextView) convertView.findViewById(R.id.tv_ball_two);
            holder.tv_ball_three = (TextView) convertView.findViewById(R.id.tv_ball_three);
            holder.tv_ball_four = (TextView) convertView.findViewById(R.id.tv_ball_four);
            holder.tv_ball_five = (TextView) convertView.findViewById(R.id.tv_ball_five);
            holder.tv_ball_six = (TextView) convertView.findViewById(R.id.tv_ball_six);
            holder.tv_ball_seven = (TextView) convertView.findViewById(R.id.tv_ball_seven);
            holder.tv_ball_eight = (TextView) convertView.findViewById(R.id.tv_ball_eight);

            holder.tv_ball_one_sfc = (TextView) convertView.findViewById(R.id.tv_ball_one_sfc);
            holder.tv_ball_two_sfc = (TextView) convertView.findViewById(R.id.tv_ball_two_sfc);
            holder.tv_ball_three_sfc = (TextView) convertView.findViewById(R.id.tv_ball_three_sfc);
            holder.tv_ball_four_sfc = (TextView) convertView.findViewById(R.id.tv_ball_four_sfc);
            holder.tv_ball_five_sfc = (TextView) convertView.findViewById(R.id.tv_ball_five_sfc);
            holder.tv_ball_six_sfc = (TextView) convertView.findViewById(R.id.tv_ball_six_sfc);
            holder.tv_ball_seven_sfc = (TextView) convertView.findViewById(R.id.tv_ball_seven_sfc);
            holder.tv_ball_eight_sfc = (TextView) convertView.findViewById(R.id.tv_ball_eight_sfc);
            holder.tv_ball_nine_sfc = (TextView) convertView.findViewById(R.id.tv_ball_nine_sfc);
            holder.tv_ball_ten_sfc = (TextView) convertView.findViewById(R.id.tv_ball_ten_sfc);
            holder.tv_ball_eleven_sfc = (TextView) convertView.findViewById(R.id.tv_ball_eleven_sfc);
            holder.tv_ball_twelve_sfc = (TextView) convertView.findViewById(R.id.tv_ball_twelve_sfc);
            holder.tv_ball_thirteen_sfc = (TextView) convertView.findViewById(R.id.tv_ball_thirteen_sfc);
            holder.tv_ball_fourteen_sfc = (TextView) convertView.findViewById(R.id.tv_ball_fourteen_sfc);
            holder.ll_sfc = (LinearLayout) convertView.findViewById(R.id.ll_sfc);

            holder.tv_jjc = (TextView) convertView.findViewById(R.id.tv_jjc);
            holder.ll_szc = (LinearLayout) convertView.findViewById(R.id.ll_szc);
            holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
            holder.tv_lotteryName = (TextView) convertView.findViewById(R.id.tv_lotteryName);

            holder.tv_history_poker_one=((TextView) convertView.findViewById(R.id.tv_history_poker_one));
            holder.tv_history_poker_two=((TextView) convertView.findViewById(R.id.tv_history_poker_two));
            holder.tv_history_poker_three=((TextView) convertView.findViewById(R.id.tv_history_poker_three));
            holder.tv_history_poker_type=((TextView) convertView.findViewById(R.id.tv_poker_type));
            holder.ll_lottery_klpk=((LinearLayout) convertView.findViewById(R.id.ll_lottery_klpk));


            holder.iv_ks_one=((ImageView) convertView.findViewById(R.id.iv_lottery_ks_one));
            holder.iv_ks_two=((ImageView) convertView.findViewById(R.id.iv_lottery_ks_two));
            holder.iv_ks_three=((ImageView) convertView.findViewById(R.id.iv_lottery_ks_three));
            holder.ll_ks=((LinearLayout) convertView.findViewById(R.id.ll_ks));

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //球类
        TextView[] texts = {holder.tv_ball_one,holder.tv_ball_two,holder.tv_ball_three, holder.tv_ball_four, holder.tv_ball_five,
                holder.tv_ball_six,holder.tv_ball_seven,holder.tv_ball_eight};
        //胜负彩
        TextView[] textsSFC = {holder.tv_ball_one_sfc,holder.tv_ball_two_sfc,holder.tv_ball_three_sfc, holder.tv_ball_four_sfc, holder.tv_ball_five_sfc,
                holder.tv_ball_six_sfc,holder.tv_ball_seven_sfc,holder.tv_ball_eight_sfc,holder.tv_ball_nine_sfc, holder.tv_ball_ten_sfc,
                holder.tv_ball_eleven_sfc,  holder.tv_ball_twelve_sfc, holder.tv_ball_thirteen_sfc,holder.tv_ball_fourteen_sfc};
        //快乐扑克
        TextView[] pokers={holder.tv_history_poker_one,holder.tv_history_poker_two,holder.tv_history_poker_three};

        //快三
        ImageView[] ivDices={holder.iv_ks_one,holder.iv_ks_two,holder.iv_ks_three};

        //获取球数据
        OpenCodeBean beans = bean.get(position);
        String result = beans.getResult();
        holder.tv_date.setText(beans.getAddtime());
        holder.tv_date.setTag(beans.getAddtime()); //时间

        holder.tv_lotteryName.setText(beans.getType());
        holder.tv_lotteryName.setTag(beans.getLotteryid());

        holder.tv_ball_one.setTag(beans.getTermNo());
        holder.tv_ball_two.setTag(beans.getName());


        //切除|后的数据
        String[] results = null;
        //切除后的红球数据
        String[] redBall = null;
        //切除后的蓝球数据
        String[] blueBall = null;
        //总数据长度
        int lenght = 0;

        if(result.contains("|")){
            results = result.split("\\|");
            redBall = results[0].split(",");
            blueBall = results[1].split(",");
            lenght = redBall.length+ blueBall.length;
        }else{
            redBall = result.split(",");
            lenght = redBall.length;
        }

        int type = Integer.parseInt(beans.getLotteryid());
        switch (type){
            case LotteryType.JCZQ:
                holder.ll_sfc.setVisibility(View.GONE);
                holder.ll_szc.setVisibility(View.GONE);
                holder.tv_jjc.setVisibility(View.VISIBLE);
                holder.ll_lottery_klpk.setVisibility(View.GONE);
                holder.ll_ks.setVisibility(View.GONE);
                setAnnouncementInfo(results,holder.tv_jjc,holder.ll_szc,LotteryType.JCZQ);
                break;
            case LotteryType.JCLQ:
                holder.ll_sfc.setVisibility(View.GONE);
                holder.ll_szc.setVisibility(View.GONE);
                holder.tv_jjc.setVisibility(View.VISIBLE);
                holder.ll_lottery_klpk.setVisibility(View.GONE);
                holder.ll_ks.setVisibility(View.GONE);
                setAnnouncementInfo(results,holder.tv_jjc,holder.ll_szc,LotteryType.JCLQ);
                break;
            case LotteryType.BJDC:
                holder.ll_sfc.setVisibility(View.GONE);
                holder.ll_szc.setVisibility(View.GONE);
                holder.tv_jjc.setVisibility(View.VISIBLE);
                holder.ll_lottery_klpk.setVisibility(View.GONE);
                holder.ll_ks.setVisibility(View.GONE);
                setAnnouncementInfo(results,holder.tv_jjc,holder.ll_szc,LotteryType.JCZQ);
                break;
            case LotteryType.SFC:
                holder.ll_sfc.setVisibility(View.VISIBLE);
                holder.ll_szc.setVisibility(View.GONE);
                holder.tv_jjc.setVisibility(View.GONE);
                holder.ll_lottery_klpk.setVisibility(View.GONE);
                holder.ll_ks.setVisibility(View.GONE);
                setAnnouncementInfo(redBall,lenght,textsSFC);
                break;
            case LotteryType.KLPK:

                holder.ll_sfc.setVisibility(View.GONE);
                holder.ll_szc.setVisibility(View.GONE);
                holder.tv_jjc.setVisibility(View.GONE);
                holder.ll_lottery_klpk.setVisibility(View.VISIBLE);
                holder.ll_ks.setVisibility(View.GONE);
                setAnnouncementInfo(beans.getResult(),pokers,holder.tv_history_poker_type);

                break;
            case LotteryType.AHKS:
            case LotteryType.HBKS:
            case LotteryType.JLKS:
            case LotteryType.JSKS:
                //这里我们将处理快三的显示；
                holder.ll_sfc.setVisibility(View.GONE);
                holder.ll_szc.setVisibility(View.GONE);
                holder.tv_jjc.setVisibility(View.GONE);
                holder.ll_lottery_klpk.setVisibility(View.GONE);
                holder.ll_ks.setVisibility(View.VISIBLE);
                setAnnouncementInfo(redBall,ivDices);
                break;
            default:
                holder.ll_sfc.setVisibility(View.GONE);
                holder.ll_szc.setVisibility(View.VISIBLE);
                holder.tv_jjc.setVisibility(View.GONE);
                holder.ll_lottery_klpk.setVisibility(View.GONE);
                holder.ll_ks.setVisibility(View.GONE);
                setAnnouncementInfo(redBall,blueBall,lenght,texts);
                break;
        }

        return convertView;
    }


    //胜负彩item复用了 七乐彩的item    初始化创建6个item  七乐彩item位于4  第10个tiem其实是第4个item
    /**
     * 胜负彩
     * @param redBall
     * @param length
     * @param textsSFC
     */
    private void setAnnouncementInfo(String[]redBall,int length,TextView[]textsSFC) {
        for (int i = 0; i < textsSFC.length; i++) {
            if (i < length) {
                textsSFC[i].setText(redBall[i]);
                textsSFC[i].setVisibility(View.VISIBLE);
            } else {
                textsSFC[i].setVisibility(View.GONE);
            }
        }
    }

    /**
     * 这里是球彩的一些东西；
     * @param redBall
     * @param blueBall
     * @param lenght
     * @param texts
     */
    private void setAnnouncementInfo(String[] redBall,String[] blueBall ,int lenght,TextView[]texts){

        for (int i = 0; i < texts.length; i++){
            if(i < lenght){
                texts[i].setVisibility(View.VISIBLE);
            }else{
                texts[i].setVisibility(View.GONE);
            }
        }
        //设置红球数据
        for (int i = 0; i < redBall.length; i++){
            texts[i].setText(redBall[i]);
            texts[i].setBackgroundResource(R.drawable.red_ball);
        }


        if(blueBall != null){
            for(int i = 0; i < blueBall.length; i++){
                texts[redBall.length +i].setText(blueBall[i]);
                texts[redBall.length +i].setBackgroundResource(R.drawable.blue_ball);
            }
        }

    }

    /**
     * 这里是设置足球彩之类的
     * @param results
     * @param tv_jjc
     * @param ll_szc
     * @param type
     */
    private void setAnnouncementInfo(String[] results,TextView tv_jjc,LinearLayout ll_szc,int type){

        if(type == LotteryType.JCZQ){
            tv_jjc.setBackgroundResource(R.drawable.jczq);
        }else if(type == LotteryType.JCLQ){
            tv_jjc.setBackgroundResource(R.drawable.jclq);
        }
        //竞技彩 | 后面的数据
        String resultEnd = results[1];
        //竞技彩 | 前面的数据
        String[] resultStart = results[0].split("VS");
        tv_jjc.setText(resultStart[0] +"　"+ resultEnd +"　"+ resultStart[1]);
    }


    /**
     * 这里是设置快乐扑克的item的
     * @param results
     * @param pokers
     */
    private void setAnnouncementInfo(String results,TextView[] pokers,TextView tvPokerType){

        List<PokerBean> pokerBeanList = KlpkUtils.getPokerBeanList(results);

        MyApplication applicationContext = (MyApplication) context.getApplicationContext();

        //设置牌型；
        for (int i = 0; i < pokerBeanList.size(); i++) {

            TextView tvPoker=pokers[i];
            tvPoker.setTypeface(applicationContext.getPokerTypeFace());
            PokerBean pokerBean = pokerBeanList.get(i);
            PokerBean.Category category = pokerBean.getCategory();


            String pokerNumber = KlpkUtils.pokerNumer[pokerBean.getNumber()];
            tvPoker.setText(pokerNumber);
            tvPoker.setBackground(context.getResources().getDrawable(KlpkUtils.pokerHistoryBgRes[category.ordinal()]));

            //如果是10的话的，paddingRight=1dp
            if("10".equals(pokerNumber)){
                tvPoker.setPadding(0,0, BaseUtils.dip2px(context,1),0);
            }else{
                tvPoker.setPadding(0,0, BaseUtils.dip2px(context,5),0);
            }


            if(category== PokerBean.Category.box||category== PokerBean.Category.hearts){
                tvPoker.setTextColor(context.getResources().getColor(R.color.klpk_text_red_color));
            }else{
                tvPoker.setTextColor(Color.BLACK);
            }
        }
        //这里我们是需要判断牌型的；
        tvPokerType.setText("形态 : "+KlpkUtils.getBrandType(pokerBeanList));
    }

    int[] dicesRes = new int[]{R.drawable.dice_one, R.drawable.dice_two, R.drawable.dice_three, R.drawable.dice_four, R.drawable.dice_five, R.drawable.dice_six};
    //快三的具体显示；
    private void setAnnouncementInfo(String[] dicesStr,ImageView[] ivDices){

        //这里我们是需要根据dicesStr来决定ivDices具体显示什么；
       List<Integer> list=new ArrayList<>();
        if (dicesStr != null) {
            for (String s : dicesStr) {
                list.add(Integer.valueOf(s));
            }
        }
        for (int i = 0; i < ivDices.length; i++) {
            ivDices[i].setImageResource(dicesRes[list.get(i)-1]);
        }

    }


    public class ViewHolder{
        TextView tv_ball_one;
        TextView tv_ball_two;
        TextView tv_ball_three;
        TextView tv_ball_four;
        TextView tv_ball_five;
        TextView tv_ball_six;
        TextView tv_ball_seven;
        TextView tv_ball_eight;


        TextView tv_ball_one_sfc;
        TextView tv_ball_two_sfc;
        TextView tv_ball_three_sfc;
        TextView tv_ball_four_sfc;
        TextView tv_ball_five_sfc;
        TextView tv_ball_six_sfc;
        TextView tv_ball_seven_sfc;
        TextView tv_ball_eight_sfc;
        TextView tv_ball_nine_sfc;
        TextView tv_ball_ten_sfc;
        TextView tv_ball_eleven_sfc;
        TextView tv_ball_twelve_sfc;
        TextView tv_ball_thirteen_sfc;
        TextView tv_ball_fourteen_sfc;
        LinearLayout ll_sfc;

        TextView tv_jjc;
        LinearLayout ll_szc;
        TextView tv_date;
        public TextView tv_lotteryName;

        TextView tv_history_poker_one;
        TextView tv_history_poker_two;
        TextView tv_history_poker_three;
        TextView tv_history_poker_type;
        LinearLayout ll_lottery_klpk;

        ImageView iv_ks_one;
        ImageView iv_ks_two;
        ImageView iv_ks_three;
        LinearLayout ll_ks;
    }
}













